/**
 * 功能区
 */
import * as mars3d from "mars3d"
import { listDivide } from "@mars/api/data/index"
const Cesium = mars3d.Cesium

let map: mars3d.Map // 地图对象
let graphicLayer: mars3d.layer.GraphicLayer
let _last_basemap: string | number | mars3d.layer.BaseTileLayer


// 初始化当前业务
export function onMounted(mapInstance: mars3d.Map): void {
  map = mapInstance // 记录map
  console.log("source-area===========")
  // 创建矢量数据图层
  graphicLayer = new mars3d.layer.GraphicLayer()
  map.addLayer(graphicLayer)
  map.setCameraView({ lat: 31.531747, lng: 120.730147, alt: 17769.2, heading: 359.9, pitch: -55.5 })
  bindLayerPopup()
}

// 释放当前业务
export function onUnmounted(): void {
  if (_last_basemap) {
    map.basemap = _last_basemap // 还原底图
    _last_basemap = null
  }
  map.cancelFlyTo()
  graphicLayer.remove()
  graphicLayer = null

  map = null
}

const pos = [{ x: 120.69405875928, y: 31.684596652849898, z: 0 }, { x: 120.69405731398506, y: 31.684596487315307, z: 0 }, { x: 120.69405875928, y: 31.684596652849898, z: 0 }, { x: 120.6945787087772, y: 31.683304923968997, z: 0 }, { x: 120.70308765937864, y: 31.684533163318264, z: 0 }, { x: 120.70932539449124, y: 31.682562074368892, z: 0 }, { x: 120.71393941045747, y: 31.68168631381894, z: 0 }, { x: 120.72061704792124, y: 31.682321611013187, z: 0 }, { x: 120.7246155585862, y: 31.682365094024913, z: 0 }, { x: 120.73175350424312, y: 31.681038261572606, z: 0 }, { x: 120.74504388892227, y: 31.681465226900915, z: 0 }, { x: 120.76112846297035, y: 31.68193813123202, z: 0 }, { x: 120.77152969643744, y: 31.682195965282872, z: 0 }, { x: 120.77197869125189, y: 31.683064974053732, z: 0 }, { x: 120.76131023603976, y: 31.6828483247012, z: 0 }, { x: 120.7551586893575, y: 31.682838372267764, z: 0 }, { x: 120.75073163054053, y: 31.682573390727566, z: 0 }, { x: 120.74367697370522, y: 31.682234755135962, z: 0 }, { x: 120.73110610634942, y: 31.68213785490575, z: 0 }, { x: 120.72202804262999, y: 31.683121965775417, z: 0 }, { x: 120.71403506600437, y: 31.68264296051572, z: 0 }, { x: 120.70382437847205, y: 31.685085340125795, z: 0 }, { x: 120.70146352493218, y: 31.6852637503546, z: 0 }, { x: 120.69669194511884, y: 31.6843969746324, z: 0 }, { x: 120.69448906886026, y: 31.6845750453636, z: -0.0006681805225129257 }, { x: 120.69405875928, y: 31.684596652849898, z: 0 }]

// 获取功能区列表
export async function getDivideList() {
  const res:any = await listDivide()
  // console.log("getDivideList========", res.rows)
  return res.rows
}

// 创建功能区
export function addSourceArea(positions, remark, labelName) {
  const graphic = new mars3d.graphic.PolygonEntity({
    positions,
    style: {
      height: 20,
      diffHeight: 50,

      materialType: mars3d.MaterialType.PolyGradient,
      materialOptions: {
        color: "#3388cc",
        alphaPower: 1.5
      },

      // 高亮时的样式（默认为鼠标移入，也可以指定type:'click'单击高亮），构造后也可以openHighlight、closeHighlight方法来手动调用
      highlight: {
        // type: "click",
        materialOptions: {
          color: "#ff0000",
          alphaPower: 1.5
        }
      },
      label: {
        text: labelName,
        font_size: 18,
        color: "#ffffff",
        distanceDisplayCondition: true,
        distanceDisplayCondition_far: 500000,
        distanceDisplayCondition_near: 0
      }
    },
    attr: remark
  })
  graphicLayer.addGraphic(graphic) // 还可以另外一种写法: graphic.addTo(graphicLayer)
}

// 在图层绑定Popup弹窗
export function bindLayerPopup() {
  graphicLayer.bindPopup(function (event) {
    const attr = event.graphic.attr || {}

    return mars3d.Util.getTemplateHtml({ title: attr["名称"], template: "all", attr })
  })
}
